<!DOCTYPE HTML>
<html>
<head>
<#include "/script.ftl">
<title>修改密码</title>
</head>
<body>
	<div class="page-container">
		<form id="form" action="updatePassword" autocomplete="off">
			<input type="hidden" name="userId" value="${user.userId!}">
			<div class="row cl ">
				<label class="form-label col-sm-6 text-r">旧密码</label>
				<div class="formControls col-sm-6">
					<input  type="password" name="password" id="password" value="" class="input-text password" >
				</div>
			</div >
			<div class="row cl mt-10">
				<label class="form-label col-sm-6 text-r">新密码</label>
				<div class="formControls col-sm-6">
					<input id="newPassword"  type="password" name="newPassword" id="newPassword" value="" class="input-text newPassword" >
				</div>
			</div >
			<div class="row cl mt-10">
				<label class="form-label col-sm-6 text-r">确认新密码</label>
				<div class="formControls col-sm-6">
					<input  type="password" name="confirmPassword" id="confirmPassword" value="" class="input-text confirmPassword" >
				</div>
			</div >
			<div class="mt-10 text-c">
				<input class="btn radius btn-primary"  type="submit" value="提交">
				<input class="btn radius btn-default"  type="button" onClick="window.parent.location.reload();" value="取消">
			</div>
		</form>
	</div>
</body>
<script>
	$("#form").validate({
	 	 rules:{  
            password : {  
                required:true,  
                minlength:6,  
                maxlength:16  
            },  
            newPassword : {  
                required:true,  
                minlength:6,  
                maxlength:16  
            },  
            confirmPassword : {  
                required:true,
                minlength:6,  
                maxlength:16,  
                equalTo: "#newPassword"
           }
        },
        messages:{
        	password:{
        		required:"请输入密码",
        		minlength:jQuery.format("密码不能小于{6}个字符"),
        		maxlength:jQuery.format("密码不能大于{16}个字符")
        	},
        	newPassword:{
        		required:"请输入新密码",
        		minlength:jQuery.format("新密码不能小于{6}个字符"),
        		maxlength:jQuery.format("新密码不能大于{16}个字符")
        	},
        	confirmPassword:{
        		required:"请输入确认新密码",
        		minlength:jQuery.format("新密码不能小于{6}个字符"),
        		maxlength:jQuery.format("新密码不能大于{16}个字符"),
        		equalTo:"两次输入密码不一致"
        	}
        },
		debug:true,
		onkeyup:false,
		focusCleanup:false,
		success:"valid",
		invalidHandler: function(form, validator) {return false;},
		submitHandler: function(form) {
			var json = $('#form').serialize();
			//var dataJson =  formToJson(json);
			//alert(dataJson) 
	        $.ajax({  
	          type : 'post',  
	          //contentType : 'application/json',  
	          url : 'updatePassword',
	          data :  json,
	          success : function(data) {
	        	  var rsult = jQuery.parseJSON(data);
	        	  if (rsult.success) {
	        		  alert("修改密码成功！");
	        		  window.location.href="../logout";
	        	  } else 
	        		  alert("修改密码失败!	" + rsult.message);
	          },  
	          error : function(data) {  
	              alert("系统错误！")  
	          }  
	        });
	        return false;
		}
	});
	//将从form中通过$('#form').serialize()获取的值转成json  
	function formToJson(data) {  
       data=data.replace(/&/g,"\",\"");  
       data=data.replace(/=/g,"\":\"");  
       data="{\""+data+"\"}";  
       return data;  
    }  
</script>
</html>